package ModelLayer;

import java.util.HashMap;
/**
 * This class is part of an address book system. It's an aggregation of class DVDCon, and contains DVD
 * data for dvds contained within the collection. 
 *
 * The purpose of this class is to supply information to the ControlLayer. 
 * 
 * @author Anders A.P, Anna K., Danny M.P., Jakob K., Mie P.  
 * @version 1.0
 */
public class DVD
{ 
    private static int              classID = 0;
    private int                     id;
    private String                  title;
    private String                  artist;
    private String                  pubDate;
    private HashMap<Integer,Copy>   copies;

    /**
     * Constructs in instance of class DVD, incrementing the classID field and takin the following parameters:
     * 
     * @param   title       DVD title as a String, not blank or null.
     * @param   artist      DVD artist as a String, not blank or null.
     * @param   pubDate     DVD publish date as a String, larger than one character.
     * @throw   Exception   If any of the strings supplied does not pass testing
     */
    public DVD(String title, String artist, String pubDate)
    {
        if(!setTitle(title)) {
            throw new IllegalArgumentException("Title contains bad data.");
        }
        if(!setArtist(artist)) {         
            throw new IllegalArgumentException("Artist contains bad data.");
        }
        if(!setPubDate(pubDate)) {         
            throw new IllegalArgumentException("Pubdate contains bad data.");
        }
        ++classID;
        setId(classID);
        copies = new HashMap<Integer, Copy>();
      
    }

    /**
     * Returns the value of the field id to the caller.
     *
     * @return  id  Returns DVD id.
     */    
    public int getID()
    {
        return id;
    }

    /**
     * Sets new information in the id field, taking the following parameters:
     *
     * @param   id      New DVD id.
     * @return  true    If id is bigger than 0.
     * @return  false   If id is less than or equal to 0.
     */    
    public boolean setId(int id)
    {        
        if(id > 0) {            
            this.id = id;
            return true;            
        }
        else {    
            return false;
        }        
    }

    /**
     * Sets new information in the title field, taking the following parameters:
     *
     * @param   title   New DVD title.
     * @return  true    If title length is bigger than 0.
     * @return  false   If title length is less than or equal to 0.
     */       
    public boolean setTitle(String title)
    {        
        if(title.trim().length() > 0) {            
            this.title = title;
            return true;            
        }
        else {            
            return false;
        }        
    }

    /**
     * Sets new information in the artist field, taking the following parameters:
     *
     * @param   artist  New DVD artist.
     * @return  true    If artist length is bigger than 0.
     * @return  false   If artist length is less than or equal to 0.
     */     
    public boolean setArtist(String artist)
    {        
        if(artist.trim().length() > 0) {            
            this.artist = artist;
            return true;            
        }
        else {            
            return false;            
        }        
    }

    /**
     * Sets new information in the pubDate field, taking the following parameters:
     *
     * @param   pubDate New DVD publish date.
     * @return  true    If pubDate length is bigger than 0.
     * @return  false   If pubDate length is less than or equal to 0.
     */     
    public boolean setPubDate(String pubDate)
    {        
        if(pubDate.trim().length() > 0) {            
            this.pubDate = pubDate;
            return true;
        }
        else {            
            return false;            
        }
    }

    /**
     * Returns the value of the field id to the caller.
     *
     * @return  id  Returns DVD id.
     */       
    public int getId()
    {
        return id;        
    }

    /**
     * Returns the value of the field title to the caller.
     *
     * @return  title   Returns DVD title.
     */     
    public String getTitle()
    {        
        return title;                
    }

    /**
     * Returns the value of the field artist to the caller.
     *
     * @return  artist  Returns DVD artist.
     */      
    public String getArtist()
    {        
        return artist;        
    }

    /**
     * Returns the value of the field pubDate to the caller.
     *
     * @return  pubDate Returns DVD pubDate.
     */      
    public String getPubDate()
    {       
        return pubDate;
    }
    
    /**
     * Adds a copy to the collection taking the following parameters:
     *
     * @param   key     The key of the mapping added to the collection.
     * @param   value   A Copy object to function as value of the mapping added to the collection.
     */
    public void addFiles(int key, Copy value)
    { 
        copies.put(key, value);
    }
    
    /**
     * Removes a copy from the collection taking the following parameter:
     *
     * @param   key The key used to get the mapping to be deleted from the collection. 
     */
    public void deleteFiles(int key)
    {        
        copies.remove(key);                
    }

    /**
     * Returns a Copy object from the collection, taking the following parameters:
     *
     * @param   key         The key used to get the mapping from which the value can be retrieved.
     * @return  Copy        Returns a Copy object matching the key supplied as a parameter.
     * @throw   Exception   If no key matches a mapping in the collection.
     */         
    public Copy getCopy(int key)
    {
        if(copies.get(key) != null) {
            return copies.get(key);
        }
        else {
            throw new IllegalArgumentException("Copy not found");
        }
        
    }

    /**
     * Returns the HashMap collection of copies to the caller.
     *
     * @return  HashMap A HasMap of the collection of copies.
     */        
    public HashMap<Integer,Copy> getCopies()
    {        
        return copies;        
    }
}

